<?php
require('../includes/class.main.php');
$manager = new MeetingManager();

include('../template/admin-header.php');

	$lookup = new MeetingLookup();
//	$lookup = new CalendarLookup();
//	$events = $lookup->getEvents();
	$db = new DB();
	
	$outcome = "";
	
	$focus_time_hm		= "";
	$focus_dayofmonth	= "";
	$focus_year			= "";


	if (!isset($_POST['submit']))
	{
		if (isset($_GET['addmode'])) // add mode, show empty form
		{
			$_SESSION['uiState'] = "ADD";
			
			$title			= "";
			$time_hm		= "";
			$time_meridian	= "";
			$time			= "";
			$dayofweek		= "";
			$month			= "";
			$dayofmonth		= "";
			$year			= "";
			$eventtext 		= array_fill(0, 10, "");
		}
		else // change or delete mode, populate form
		{
			$rownr = $_GET['row'];
			$_SESSION['drow'] = $rownr;
			$_SESSION['uiState'] = "CHANGE_DEL";

			$eventrow = $db->setQuery("select * from calendar where row = $rownr");
			$row = $eventrow->fetch_assoc();
			
			$title			= $row['title'];

			$time			= $row['time'];
			$time			= strtoupper(trim($time));
			$time			= preg_replace('/\s\s+/', ' ', $time); 		// remove excess whitespace
			$time			= preg_replace('/([^ ])A/', '\1 A', $time);	// add space before AM or PM if necessary
			$time			= preg_replace('/([^ ])P/', '\1 P', $time);
			$time_hm		= substr($time, 0, strpos($time, ' '));
			if (preg_match('/^([1-9]|(10|11|12))$/', $time_hm)) $time_hm .= ":00";	// supply ":00" if lacking minutes
			$time_meridian	= substr($time, strpos($time, ' ') + 1, strlen($time) - strpos($time, ' ') - 1);

			$dayofweek		= $row['dayofWeek'];
			$month			= $row['month'];
			$dayofmonth		= $row['dayOfMonth'];
			$year			= $row['year'];
			$eventtext 		= array_fill(0, 10, "");
			$eventtext[0]	= $row['line1'];
			$eventtext[1]	= $row['line2'];
			$eventtext[2]	= $row['line3'];
			$eventtext[3]	= $row['line4'];
			$eventtext[4]	= $row['line5'];
			$eventtext[5]	= $row['line6'];
			$eventtext[6]	= $row['line7'];
			$eventtext[7]	= $row['line8'];
			$eventtext[8]	= $row['line9'];
			$eventtext[9]	= $row['line10'];
		}
	}
	else // Form was posted for meeting deletion, change, or addition (or operation was cancelled)
	{
		$focus_time_hm		= $_SESSION['focus_time_hm'];
		$focus_dayofmonth	= $_SESSION['focus_dayofmonth'];
		$focus_year			= $_SESSION['focus_year'];

		$title			= $_POST['title'];
		$time_hm		= trim($_POST['time_hm']);
		if (preg_match('/^([1-9]|(10|11|12))$/', $time_hm)) $time_hm .= ":00";	// supply ":00" if lacking minutes
		$time_meridian	= $_POST['time_meridian'];
		$time			= $time_hm . " " . $time_meridian;
		$dayofweek		= $_POST['dayofweek'];
		$month			= $_POST['month'];
		$dayofmonth		= $_POST['dayofmonth'];
		$year			= $_POST['year'];
		$eventtext[0]	= $_POST['line1'];
		$eventtext[1]	= $_POST['line2'];
		$eventtext[2]	= $_POST['line3'];
		$eventtext[3]	= $_POST['line4'];
		$eventtext[4]	= $_POST['line5'];
		$eventtext[5]	= $_POST['line6'];
		$eventtext[6]	= $_POST['line7'];
		$eventtext[7]	= $_POST['line8'];
		$eventtext[8]	= $_POST['line9'];
		$eventtext[9]	= $_POST['line10'];
		
		if ($_POST['submit'] == "Delete Event")
		{
			// delete row and set message
			$rownr = $_SESSION['drow'];
			$query_result = $db->setQuery("delete from calendar where row = $rownr");

			$dberr = $db->getError();
			if ($dberr == "")
				$outcome = "<a href=\"index.php\" style='color: blue;'>Event deleted, click 'Continue' to proceed.</a>";

			else
				$outcome = "<p style='color: red;'>Operation failed. [advice=$dberr]</p>";
				
			$_SESSION['uiState'] = "CONTINUE_DELETE";
		}
		else // adding or changing
		{
			// Standardize and validate form information
			$focus_time_hm		= "";
			$focus_dayofmonth	= "";
			$focus_year			= "";
			
			$errortext = "";
			if (!preg_match('/^([1-9]|(10|11|12)):[0-5][0-9]$/', $time_hm))
			{
				$errortext .= "<li>Please correct <i>Time</i>; it must be in the form 'h:mm' ('h' may be 1-9, 10, 11, or 12). ";
				$focus_time_hm = "autofocus";
			}

			$monthnum = array_search($month, array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")) + 1;
			if (!checkdate($monthnum, intval($dayofmonth), intval($year)))
			{
				$errortext .= "<li>Please correct day of month. ";
				$focus_dayofmonth = "autofocus";
			}
			
			$dateA = getdate();
			if ($year != $dateA['year'] && $year != ($dateA['year'] + 1))
			{
				$errortext .= "<li>Please correct year (must be " . $dateA['year'] . " or " . ($dateA['year'] + 1) . "). ";
				$focus_year = "autofocus";
			}

			$_SESSION['focus_time_hm']		= $focus_time_hm;
			$_SESSION['focus_dayofmonth']	= $focus_dayofmonth;
			$_SESSION['focus_year']			= $focus_year;

			if ($errortext == "")
			{
				if ($_POST['submit'] == "Add Event")
				{
					// add row and set message
					$query_result = $db->setQuery("insert into calendar "
						. "(title, time, dayofWeek, month, dayOfMonth, year, line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, updated) "
						. "values (\"$title\",\"$time\",\"$dayofweek\",\"$month\",\"$dayofmonth\",\"$year\","
						. "\"$eventtext[0]\","
						. "\"$eventtext[1]\","
						. "\"$eventtext[2]\","
						. "\"$eventtext[3]\","
						. "\"$eventtext[4]\","
						. "\"$eventtext[5]\","
						. "\"$eventtext[6]\","
						. "\"$eventtext[7]\","
						. "\"$eventtext[8]\","
						. "\"$eventtext[9]\", NOW())");

					$dberr = $db->getError();
					if ($dberr != "")
						$outcome ="<p style='color: red;'>Addition failed. [advice=$dberr]</p>";
					else
						$outcome = "<a href=\"index.php\" style='color: blue;'>Event added, click 'Continue' to proceed.</a>";

					$_SESSION['uiState'] = "CONTINUE_ADD";
				}
				else if ($_POST['submit'] == 'Save Changes')
				{
					// update row and set message
					$rownr = $_SESSION['drow'];
					echo $time . ", " . $rownr;
					$query_result = $db->setQuery("update calendar SET "
						. "title=\"$title\","
						. "time=\"$time\","
						. "dayofWeek=\"$dayofweek\","
						. "month=\"$month\","
						. "dayOfMonth=\"$dayofmonth\","
						. "year=\"$year\","
						. "line1=\"$eventtext[0]\","
						. "line2=\"$eventtext[1]\","
						. "line3=\"$eventtext[2]\","
						. "line4=\"$eventtext[3]\","
						. "line5=\"$eventtext[4]\","
						. "line6=\"$eventtext[5]\","
						. "line7=\"$eventtext[6]\","
						. "line8=\"$eventtext[7]\","
						. "line9=\"$eventtext[8]\","
						. "line10=\"$eventtext[9]\", updated=NOW()"
						. " where row = $rownr");

					$dberr = $db->getError();
					if ($dberr != "")
						$outcome = "<p style='color: red;'>Update failed. [advice=$dberr]</p>";
					else
						$outcome = "<a href=\"index.php\" style='color: blue;'>Event changed, click 'Continue' to proceed.</a>";

					$_SESSION['uiState'] = "CONTINUE_CHANGE_DEL";
				}
			}
			else
			{
				$outcome = "<p><ul style='color: red;'>" . $errortext . "</ul></p>";
			}
		}
	}

	$abled = "";
	if (isset($_SESSION['uiState']))
	{
		if ($_SESSION['uiState'] == "CONTINUE_ADD"
			|| $_SESSION['uiState'] == "CONTINUE_DELETE"
			|| $_SESSION['uiState'] == "CONTINUE_CHANGE_DEL") $abled = "disabled";
	}
	
	$ts = 80;
?>

<div class="left">
<?php 
include('../admin/menu.php');
?>

</div>

<div class="right">
	<h1> <b><span class="blue">
	<?php if ($_SESSION['uiState'] == "ADD") { ?>
		Add Event
	<?php } else if($_SESSION['uiState'] == "CONTINUE_ADD") { ?>
		(Event Added)
	<?php } else if ($_SESSION['uiState'] == "CONTINUE_DELETE") { ?>
		(Event Deleted)
	<?php } else if ($_SESSION['uiState'] == "CONTINUE_CHANGE_DEL") { ?>
		(Event Changed)
	<?php } else { ?>
		Change or Delete Event
	<?php } ?>
	</span></b>
	</h1>

	<?php 
	if (isset($outcome) && $outcome != "") {
		echo $outcome; 
	}
	?>
	<br>
	
	<form action="editevent.php" method="post">
		<div class="adminForm">
		
			<span class="label longCol1"><b>Title</b></span>
			<input type="text" class="required" name="title" required maxlength="255" style="width:26em" <?php echo $abled; ?> value="<?php echo $title; ?>" />
			
			<br>
			<span class="label longCol1"><b>Day of Week</b></span>
			<select class="required" name="dayofweek" <?php echo $abled; ?> >
				<?php if ($dayofweek == "") $dayofweek="Sunday"; ?>
				<option <?php if ($dayofweek == "Sunday") echo "value='$dayofweek' selected"; ?>>Sunday</option>
				<option <?php if ($dayofweek == "Monday") echo "value='$dayofweek' selected"; ?>>Monday</option>
				<option <?php if ($dayofweek == "Tuesday") echo "value='$dayofweek' selected"; ?>>Tuesday</option>
				<option <?php if ($dayofweek == "Wednesday") echo "value='$dayofweek' selected"; ?>>Wednesday</option>
				<option <?php if ($dayofweek == "Thursday") echo "value='$dayofweek' selected"; ?>>Thursday</option>
				<option <?php if ($dayofweek == "Friday") echo "value='$dayofweek' selected"; ?>>Friday</option>
				<option <?php if ($dayofweek == "Saturday") echo "value='$dayofweek' selected"; ?>>Saturday</option>
			</select>
			
			<span class="label"><b>Time</b></span>
			<input class="required" type="text" name="time_hm" required maxlength="255" style="width:3.5em" <?php echo $abled . " " . $focus_time_hm; ?> value="<?php echo $time_hm; ?>">
			<select class="required" name="time_meridian" <?php echo $abled; ?> >
				<?php if ($time_meridian == "") $time_meridan="PM"; ?> 
				<option <?php if ($time_meridian == "AM") echo "value='$time_meridian' selected"; ?>>AM</option>
				<option <?php if ($time_meridian == "PM") echo "value='$time_meridian' selected"; ?>>PM</option>
			</select>
			
			<br>
			<span class="label longCol1"><b>Day of Month</b></span>
			<input class="required" type="text" name="dayofmonth" required maxlength="255" style="width:2em" <?php echo $abled . " " . $focus_dayofmonth; ?> value="<?php echo $dayofmonth; ?>" />
			
			<span class="label"><b>Month</b></span>
			<select class="required" name="month" <?php echo $abled; ?> >
				<?php if ($month == "") $month = "January"; ?>
				<option <?php if ($month == "January") echo "value='$month' selected"; ?>>January</option>
				<option <?php if ($month == "February") echo "value='$month' selected"; ?>>February</option>
				<option <?php if ($month == "March") echo "value='$month' selected"; ?>>March</option>
				<option <?php if ($month == "April") echo "value='$month' selected"; ?>>April</option>
				<option <?php if ($month == "May") echo "value='$month' selected"; ?>>May</option>
				<option <?php if ($month == "June") echo "value='$month' selected"; ?>>June</option>
				<option <?php if ($month == "July") echo "value='$month' selected"; ?>>July</option>
				<option <?php if ($month == "August") echo "value='$month' selected"; ?>>August</option>
				<option <?php if ($month == "September") echo "value='$month' selected"; ?>>September</option>
				<option <?php if ($month == "October") echo "value='$month' selected"; ?>>October</option>
				<option <?php if ($month == "November") echo "value='$month' selected"; ?>>November</option>
				<option <?php if ($month == "December") echo "value='$month' selected"; ?>>December</option>
		    </select>
		
			<span class="label"><b>Year</b></span>
			<input class="required" type="text" name="year" required maxlength="255" style="width:3em" <?php echo $abled . " " . $focus_year; ?> value="<?php echo $year; ?>"/>
			<br>
			
			<span class="label longCol1">Line 1</span>
			<input type="text" name="line1" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[0]; ?>" />
			<br>

			<span class="label longCol1">Line 2</span>
			<input type="text" name="line2" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[1]; ?>" />
			<br>

			<span class="label longCol1">Line 3</span>
			<input type="text" name="line3" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[2]; ?>" />
			<br>

			<span class="label longCol1">Line 4</span>
			<input type="text" name="line4" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[3]; ?>" />
			<br>

			<span class="label longCol1">Line 5</span>
			<input type="text" name="line5" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[4]; ?>" />
			<br>
			
			<span class="label longCol1">Line 6</span>
			<input type="text" name="line6" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[5]; ?>" />
			<br>
			
			<span class="label longCol1">Line 7</span>
			<input type="text" name="line7" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[6]; ?>" />
			<br>

			<span class="label longCol1">Line 8</span>
			<input type="text" name="line8" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[7]; ?>" />
			<br>

			<span class="label longCol1">Line 9</span>
			<input type="text" name="line9" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[8]; ?>" />
			<br>

			<span class="label longCol1">Line 10</span>
			<input type="text" name="line10" maxlength="255" style="width:26em"  <?php echo $abled; ?> value="<?php echo $eventtext[9]; ?>" />

			<br>
			<br>
			<div class="submitBlock">
				<?php if ($_SESSION['uiState'] == "CHANGE_DEL") { ?>
				<input type="submit" value="Save Changes" name="submit">
				<input type="submit" value="Delete Event" name="submit">
				<a class="label" href="index.php" style='color: red;'>Cancel</a>
				<?php } else if ($_SESSION['uiState'] == "ADD") { ?>
				<input type="submit" value="Add Event" name="submit">
				<a class="label" href="index.php" style='color: red;'>Cancel</a>
				<?php } else { ?>
				<a class="label" href="index.php" style='color: blue;'>Continue</a>
				<?php } ?>
			</div>
		</div>
	</form>
</div>

<?php 
include('../template/admin-footer.php'); 
?>
	
</body>
</html>
